package com.cpi.basilos.silo.converter;

import org.apache.log4j.Logger;

import com.cpi.basilos.corntype.converter.CornTypeConverter;
import com.cpi.basilos.corntype.view.CornTypeView;
import com.cpi.basilos.silo.dto.SiloEntryDTO;
import com.cpi.basilos.silo.dto.SiloFiltersDTO;
import com.cpi.basilos.silo.dto.SiloOutputDTO;
import com.cpi.basilos.silo.exception.SiloException;
import com.cpi.basilos.silo.view.SiloEntityView;
import com.cpi.basilos.silo.view.SiloFiltersView;
import com.cpi.basilos.silo.view.SiloOutputView;
import com.cs.common.exception.CSException;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.StringUtils;
import com.cs.common.utils.TimeUtils;

public class SiloConverter {
	private static final String SILOS_INIT_HOUR = "SILOS_INIT_HOUR";//$NON-NLS-1$
	private static final String SILOS_FINISH_HOUR = "SILOS_INIT_HOUR";//$NON-NLS-1$
	private static org.apache.log4j.Logger logger = Logger.getLogger(SiloConverter.class);
	
	public SiloFiltersDTO convertFiltersViewToDTO(SiloFiltersView siloFiltersView)throws SiloException{
		SiloFiltersDTO siloFiltersDTO = null;
		StringBuffer strDate = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		try{
			systemParameterCollection = SystemParameterCollection.getInstance();
			siloFiltersDTO = new SiloFiltersDTO();
			siloFiltersDTO.setWarehouseId(siloFiltersView.getWarehouseId());
			
			systemParameterBean = systemParameterCollection.getSystemParameterBean(SILOS_INIT_HOUR);
			strDate = new StringBuffer(16);
			strDate.append(TimeUtils.convertJavaDateToStringDate(siloFiltersView.getInitDate().getTime(), TimeUtils.DATE_PATTERN_YYYYMMDD));
			strDate.append(StringUtils.SPACE_CHAR);
			strDate.append(systemParameterBean.getValue());
			siloFiltersDTO.setInitDate(strDate.toString());
			
			systemParameterBean = systemParameterCollection.getSystemParameterBean(SILOS_FINISH_HOUR);
			strDate = new StringBuffer(16);
			strDate.append(TimeUtils.convertJavaDateToStringDate(siloFiltersView.getEndDate().getTime(), TimeUtils.DATE_PATTERN_YYYYMMDD));
			strDate.append(StringUtils.SPACE_CHAR);
			strDate.append(systemParameterBean.getValue());
			siloFiltersDTO.setEndDate(strDate.toString());
		}catch( Exception exception ){
			SiloException siloException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			siloException = new SiloException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloFiltersDTO;
	}
	
	public SiloEntityView convertEntryDTOToView(SiloEntryDTO siloEntryDTO)throws SiloException{
		SiloEntityView siloEntryView = null;
		CornTypeConverter cornTypeConverter = null;
		CornTypeView cornTypeView = null;
		try{
			cornTypeConverter = new CornTypeConverter();
			siloEntryView = new SiloEntityView();
			siloEntryView.setDate(TimeUtils.convertJavaDateToString(siloEntryDTO.getDate().getTime()));
			siloEntryView.setCompletedDate(TimeUtils.convertJavaDateToStringDate(siloEntryDTO.getDate().getTime(), TimeUtils.DATE_PATTERN_DDMMYYYYHHMM));
			siloEntryView.setAmount(NumberUtil.convertQuantity(siloEntryDTO.getAmount()));
			siloEntryView.setAmountTon(NumberUtil.convertQuantity(siloEntryDTO.getAmount()/1000));
			cornTypeView = cornTypeConverter.convertDTOToView(siloEntryDTO.getCornTypeDTO());
			siloEntryView.setCornTypeView(cornTypeView);
			siloEntryView.setOrigin(siloEntryDTO.getOrigin());
			siloEntryView.setWetness(NumberUtil.convertQuantity(siloEntryDTO.getWetness()));
			siloEntryView.setWagon(siloEntryDTO.getFurg());
		}catch( Exception exception ){
			SiloException siloException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			siloException = new SiloException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloEntryView;
	}

	public SiloOutputView convertOutputDTOToView(SiloOutputDTO siloOutputDTO)throws SiloException{
		SiloOutputView siloOutputView = null;
		CornTypeConverter cornTypeConverter = null;
		CornTypeView cornTypeView = null;
		try{
			cornTypeConverter = new CornTypeConverter();
			siloOutputView = new SiloOutputView();
			siloOutputView.setId(siloOutputDTO.getId());
			siloOutputView.setDate(TimeUtils.convertJavaDateToStringDate(siloOutputDTO.getDate().getTime(), TimeUtils.DATE_PATTERN_DDMMYYYYHHMM));
			siloOutputView.setAmount(NumberUtil.convertQuantity(siloOutputDTO.getAmount()));
			siloOutputView.setAmountTon(NumberUtil.convertQuantity(siloOutputDTO.getAmount()/1000));
			cornTypeView = cornTypeConverter.convertDTOToView(siloOutputDTO.getCornTypeDTO());
			siloOutputView.setCornTypeView(cornTypeView);
		}catch( Exception exception ){
			SiloException siloException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			siloException = new SiloException(exception,CSException.LAYER_CONVERTER, CSException.ACTION_SELECT);
			throw siloException;
		}
		return siloOutputView;
	}
}